package com.vf.admin.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.client.service.AppMingxiJinbiService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMingxiJinbi;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.platform.dao.UserDao;
import com.vf.admin.platform.dao.UserRoleDao;
import com.vf.admin.platform.dto.OrgUserDto;
import com.vf.admin.platform.entity.User;
import com.vf.admin.platform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userdao;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private AppMingxiJinbiService appMingxiJinbiService;
    @Autowired
    AppUserService appUserService;

    @Override
    public void save(User user) {
        userdao.save(user);
    }

    @Override
    public User get(Integer id) {
        return userdao.get(id);
    }

    @Override
    public List<User> findList(Map<String, Object> paraMap, SplitPageBean splitPage) {
        return userdao.findList(paraMap, splitPage);
    }

    @Override
    public void del(int id) {
        userdao.del(id);
        userRoleDao.delByUserId(id);
    }

    @Override
    public List<OrgUserDto> getOrgUserDtoList(Map<String, Object> paraMap, String time, SplitPageBean splitPage) {

        //查询用户列表
        List<User> users = userdao.findOrgUserList(paraMap, splitPage);

        List<OrgUserDto> tempList = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(users) && users.size() > 0) {

            //取出code
            users.forEach(e1 -> {
                //code
                String code = e1.getCode();
                //查询出底下所用户信息
                List<AppUser> appUsers = appUserService.getInvitationCode(code);

                List<String> list = new ArrayList<>();

                appUsers.forEach(e -> {
                    list.add(String.valueOf(e.getId()));
                });
                String join = String.join(",", list);


                //Map
                Map<String, Object> params;  //自定义时间

                Map<String, Object> allParams; //全部时间

                params = new HashMap<>();

                if (ObjectUtil.isNotEmpty(time)) {

                    params.put("today", time);
                    params.put("userids", join);
                    params.put("leixing", 1);
                } else {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    String format = formatter.format(new Date());
                    params.put("today", format);
                    params.put("userids", join);
                    params.put("leixing", 1);
                }

                allParams = new HashMap<>();

                if (ObjectUtil.isNotEmpty(time)) {
                    String hms = " 23:59:59";
                    String allTime = time + hms;
                    allParams.put("alltoday", allTime);
                    allParams.put("userids", join);
                    allParams.put("leixing", 1);
                } else {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String format = formatter.format(new Date());
                    allParams.put("alltoday", format);
                    allParams.put("userids", join);
                    allParams.put("leixing", 1);
                }

                //查询礼物数据表
                List<AppMingxiJinbi> todayMingXis = appMingxiJinbiService.indexCount(params);
                List<AppMingxiJinbi> allMingXis = appMingxiJinbiService.indexCount(allParams);

                //计算今日礼物收入
                List<AppMingxiJinbi> gift = new ArrayList<>();
                Double todayGift = 0d;
                if (ObjectUtil.isNotEmpty(todayMingXis)) {
                    todayMingXis.forEach(e -> {
                        boolean b = e.getBeizhu().equals("礼物收入");
                        if (b) {
                            gift.add(e);
                        }
                    });
                    todayGift = gift.stream().mapToDouble(e -> Double.valueOf(e.getNum().stripTrailingZeros().toPlainString())).sum();
                }

                //指定日期
                //计算全部礼物收入
                List<AppMingxiJinbi> giftAll = new ArrayList<>();
                Double allGift = 0d;
                if (ObjectUtil.isNotEmpty(allMingXis)) {
                    allMingXis.forEach(e -> {
                        boolean b = e.getBeizhu().equals("礼物收入");
                        if (b) {
                            giftAll.add(e);
                        }
                    });
                    allGift = giftAll.stream().mapToDouble(e -> Double.valueOf(e.getNum().stripTrailingZeros().toPlainString())).sum();

                }


                //计算今日音乐视频收入
                List<AppMingxiJinbi> audioVideoPrices = new ArrayList<>();
                Double audioVideoPrice = 0d;
                if (ObjectUtil.isNotEmpty(todayMingXis)) {
                    todayMingXis.forEach(e -> {
                        boolean b = e.getBeizhu().equals("语音消息收入");
                        if (b) {
                            audioVideoPrices.add(e);
                        }
                        boolean b1 = e.getBeizhu().equals("视频消息收入");
                        if (b1) {
                            audioVideoPrices.add(e);
                        }
                    });


                    audioVideoPrice = audioVideoPrices.stream().mapToDouble(e -> Double.valueOf(e.getNum().stripTrailingZeros().toPlainString())).sum();
                }


                //计算全部音乐视频收入
                List<AppMingxiJinbi> allAudioVideoPrices = new ArrayList<>();
                Double allAudioVideoPrice = 0d;
                if (ObjectUtil.isNotEmpty(todayMingXis)) {

                    todayMingXis.forEach(e -> {
                        boolean b = e.getBeizhu().equals("语音消息收入");
                        if (b) {
                            allAudioVideoPrices.add(e);
                        }
                        boolean b1 = e.getBeizhu().equals("视频消息收入");
                        if (b1) {
                            allAudioVideoPrices.add(e);
                        }
                    });

                    allAudioVideoPrice = audioVideoPrices.stream().mapToDouble(e -> Double.valueOf(e.getNum().stripTrailingZeros().toPlainString())).sum();

                }
                //数据封装处理
                OrgUserDto orgUserDto = new OrgUserDto();

                orgUserDto.setId(e1.getId());
                orgUserDto.setLoginname(e1.getLoginname());
                orgUserDto.setComments(e1.getComments());
                orgUserDto.setName(e1.getName());
                orgUserDto.setTodayGiftNum(todayGift);
                orgUserDto.setAllGiftNum(allGift);
                orgUserDto.setTodayRtcNum(audioVideoPrice);
                orgUserDto.setAllRtcNum(allAudioVideoPrice);
                orgUserDto.setCode(e1.getCode());
                tempList.add(orgUserDto);

            });
            return tempList;
        }

        return null;
    }
}
